// Thud - a dynamic object-oriented language
// Copyright (C) 2009,2010 David H. Hovemeyer <david.hovemeyer@gmail.com>

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

/*
 * MethodAssembler.cpp
 *
 *  Created on: Dec 2, 2009
 *      Author: dhovemey
 */

#include <cassert>
#include <cstdio>
#include "vm/VirtualMachineException.h"
#include "vm/Method.h"
#include "vm/MethodAssembler.h"

MethodAssembler::MethodAssembler(const std::wstring &methodName, uint32_t flags)
	: m_method(new Method(methodName))
	, m_finished(false)
	, m_labelCount(0)
	, m_lastOpcode(OP_NOP)
{
	m_method->setFlags(flags);
}

MethodAssembler::~MethodAssembler()
{
}

void MethodAssembler::addParam(const std::wstring &paramName)
{
	m_method->addParam(paramName);
}

void MethodAssembler::setNumLocals(uint8_t numLocals)
{
	m_method->setNumLocals(numLocals);
}

void MethodAssembler::emitInstruction(Opcode opcode)
{
	assert(::opcodeGetFlags(opcode) == 0);
	emitByte(opcode);
	m_lastOpcode = opcode;
}

void MethodAssembler::emitI8Instruction(Opcode opcode, int8_t i8)
{
	assert(::opcodeGetFlags(opcode) == OP_FLAG_I8_CONST);
	emitByte(opcode);
	m_lastOpcode = opcode;
	emitByte(i8);
}

void MethodAssembler::emitCPInstruction(Opcode opcode, uint16_t cpIndex)
{
	assert(::opcodeGetFlags(opcode) == OP_FLAG_CP_REF);
	emitByte(opcode);
	m_lastOpcode = opcode;
	emitShort(cpIndex);
}

void MethodAssembler::emitCPI8Instruction(Opcode opcode, uint16_t cpIndex, int8_t i8)
{
	assert(::opcodeGetFlags(opcode) == (OP_FLAG_CP_REF | OP_FLAG_I8_CONST));
	emitByte(opcode);
	m_lastOpcode = opcode;
	emitShort(cpIndex);
	emitByte(i8);
}

std::wstring MethodAssembler::genLabel()
{
	wchar_t buf[50];
	::swprintf(buf, sizeof(buf), L"l%d", m_labelCount);
	m_labelCount++;
	return std::wstring(buf);
}

std::wstring MethodAssembler::emitBranchInstruction(Opcode opcode)
{
	return emitBranchInstruction(opcode, genLabel());
}

std::wstring MethodAssembler::emitBranchInstruction(Opcode opcode, const std::wstring &label)
{
	assert(::opcodeGetFlags(opcode) == OP_FLAG_BRANCH);

	// emit instruction opcode
	emitByte(opcode);
	m_lastOpcode = opcode;

	// create a fixup which will indicate where the
	// target address will need to be stored when the label is
	// (eventually) defined
	uint16_t offset = (uint16_t) m_method->getNumBytes();
	m_fixupList.push_back(MethodFixup(offset, label));

	// emit dummy branch offset for now - will be fixed up later
	emitShort(0);

	return label;
}

void MethodAssembler::defineLabel(const std::wstring &label)
{
	m_labelMap[label] = (uint16_t) m_method->getNumBytes();
}

Opcode MethodAssembler::getLastOpcode()
{
	return m_lastOpcode;
}

void MethodAssembler::finish()
{
	assert(!m_finished);

	for (FixupList::iterator i = m_fixupList.begin(); i != m_fixupList.end(); i++) {
		LabelMap::iterator j = m_labelMap.find((*i).label);
		if (j == m_labelMap.end()) {
			throw VirtualMachineException(L"Reference to undefined label " + (*i).label);
		}
		uint16_t fixupOffset = (*i).offset;
		uint16_t targetOffset = (*j).second;

		m_method->setByte(fixupOffset, (targetOffset >> 8) & 0xFF);
		m_method->setByte(fixupOffset + 1, targetOffset & 0xFF);
	}

	m_finished = true;
}

void MethodAssembler::emitByte(int8_t b)
{
	m_method->append(b);
}

void MethodAssembler::emitShort(uint16_t s)
{
	m_method->append((s >> 8) & 0xFF);
	m_method->append(s & 0xFF);
}
